package com.zqweb.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zqweb.gulimall.product.entity.ProductAttrValueEntity;
import com.zqweb.gulimall.product.service.ProductAttrValueService;
import com.zqweb.gulimall.product.vo.AttrRespVo;
import com.zqweb.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import com.zqweb.gulimall.product.service.AttrService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;



/**
 * 商品属性
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 18:38:54
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService attrValueService;

    /**
     * 查询出指定spuId的所有基本attr记录
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getBaseAttrListForSpu(@PathVariable("spuId") Long spuId){
//        System.out.println("进入listforspu方法...");
        List<ProductAttrValueEntity> entities = attrValueService.getBaseAttrListForSpu(spuId);
        return R.ok().put("data",entities);
    }

    /**
     * 更新指定spuId相关的所有基本atrr记录
     */
    @PostMapping("/update/{spuId}")
    public R updateBySpuId(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        attrValueService.updateBySpuId(spuId,entities);
        return R.ok();
    }


    /**
     * 规格参数之分页查询（根据catId的情况是否进行catId的等值条件查询、携带查询参数key的进一步查询）
     */
    @RequestMapping("/{attrType}/list/{cateId}")
    public R baseList(@RequestParam Map<String, Object> params,@PathVariable("attrType") String attrType, @PathVariable("cateId") Long cateId){
        PageUtils page = attrService.baseQueryPage(params, cateId, attrType);
        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    /*
     该接口提供给远程服务调用，由于远程服务的不稳定、长途等因素，避免多次重复调用，可将其获取的数据存入缓存
     这里为每个attrId都创建了缓存
      */
    @Cacheable(value = "attr-zone",key = "'attrInfo:' + #root.args[0]")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attr = attrService.getByIdWithDetails(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
//		attrService.save(attr);
        attrService.saveDetail(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);
        attrService.updateDetailsById(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
